package com.china08.yxyapi.api.manage.edu.platform.continuedu.tongji;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.service.continuedu.YxyContinueduRenWuService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduTongJiListController extends YxyApiBaseController {

    @Autowired
    YxyContinueduRenWuService renwuService;

    @Autowired
    YxyContinueduTaskLessonRepository taskLessonRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/tongji/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String keyword = reqModel.keyword;
        keyword = StringTrimUtil.trim(keyword);
        keyword = (StringUtils.isNotBlank(keyword)) ? "%" + keyword + "%" : "";
        String orderBy = reqModel.orderBy;
        String orderByStatus = reqModel.orderByStatus;
        Integer pageNum = reqModel.pageNum;
        String schoolId = getProfile(request, response).getSchoolId();
        Map<String, Object> map = renwuService.renwuList(schoolId, keyword, orderBy, orderByStatus, pageNum);

        Integer allPage = 0;
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        if (null != map) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }
        List<Pojo> returnList = new ArrayList<>();
        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            String taskId = m.get("id") + "";
            pojo.id = taskId;
            pojo.name = m.get("name") + "";

            Object obj1 = m.get("begin_date");
            Object obj2 = m.get("end_date");
            if (obj1 != null) {
                pojo.beginDate = DateToolUtils.format((Date) obj1, "yyyy-MM-dd");
            }
            if (obj2 != null) {
                pojo.endDate = DateToolUtils.format((Date) obj2, "yyyy-MM-dd");
            }
            pojo.applyCount = Long.parseLong(m.get("apply_count") + "");
            pojo.completeCount = Long.parseLong(m.get("complete_count") + "");
            pojo.completeRate = (double) m.get("complete_rate");
            pojo.status = Integer.parseInt(m.get("status") + "");
            Integer lessonCount = taskLessonRepository.countByTaskId(taskId);
            pojo.lessonCount = lessonCount;
            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = returnList;
        return respModel;
    }

    public static class Pojo {
        public String id;
        public String name;
        public String beginDate;
        public String endDate;
        public long applyCount;
        public long completeCount;
        public double completeRate;
        public Integer status;
        public Integer lessonCount;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String keyword;
        public String orderBy;
        public String orderByStatus;
        public Integer pageNum;

        @Override
        public void autoCheck() {
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allPage;
        public List<Pojo> list;
    }
}
